//
//  PlanView.m
//  Tunnel
//
//  Created by wbb on 2025/6/17.
//

#import "PlanView.h"
@interface PlanCell: TYBaseTableViewCell
@property (nonatomic, strong) UILabel * titleLab;
@property (nonatomic, strong) UILabel * detailLab;
@end
@implementation PlanCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.backgroundColor = [UIColor clearColor];
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    TYBoxView *tipView = [[TYBoxView alloc] init];
    [self.contentView addSubview:tipView];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.top.offset(0);
        make.bottom.offset(-15);
    }];
    
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"运行值班长";
    titleLab.textColor = main_text_color;
    titleLab.font = [UIFont systemFontOfSize:17];
    [tipView addSubview:titleLab];
    self.titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.offset(10);
        make.right.offset(-15);
    }];
    
    UILabel *detailLab = [[UILabel alloc] init];
    detailLab.text = @"事故现场情况严峻，有两人重伤，一人死亡。车龙拥堵至洞 外10米。事故现场情况严峻，有两人重伤，一人死亡。车龙拥堵至洞 外10米。事故现场情况严峻，有两人重伤，一人死亡。车龙拥堵至洞 外10米。事故现场情况严峻，有两人重伤，一人死亡。车龙拥堵至洞 外10米。事故现场情况严峻，有两人重伤，一人死亡。车龙拥堵至洞 外10米。";
    detailLab.textColor = main_light_text_color_6;
    detailLab.numberOfLines = 0;
    detailLab.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:detailLab];
    self.detailLab = detailLab;
    [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.top.equalTo(titleLab.mas_bottom).offset(10);
        make.bottom.equalTo(tipView.mas_bottom).offset(-10);
    }];
    
}
-(void)cellWithData:(id)data {
    YJLCModel * model = data;
    self.titleLab.text = model.yjdwName;
    self.detailLab.text = model.gznr;
}
@end


@interface PlanView()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) UILabel * tipLab;
@property (nonatomic, strong) NSMutableArray * sectionStates;

@property (nonatomic, strong) NSMutableDictionary * cellDic;
@property (nonatomic, strong) YJXYModel*  model;
@property (nonatomic, strong) NSArray*  filterArr;

@end
@implementation PlanView

- (instancetype)init {
    self = [super init];
    if(self) {
        self.cellDic = [NSMutableDictionary dictionaryWithCapacity:0];
        [self initUI];
    }
    return self;
}
- (instancetype)initWithLcArr:(NSArray*)array filterArr:(NSArray*)filterArr sjModel:(YJXYModel*)model {
    self = [super init];
    if(self) {
        self.cellDic = [NSMutableDictionary dictionaryWithCapacity:0];
        self.dataArr = array.mutableCopy;
        self.filterArr = filterArr;
        self.model = model;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 55)];
    
    TYBoxView *tipView = [[TYBoxView alloc] initWithFrame:CGRectMake(12, 0, kSCREEN_WIDTH-24, 40)];
    [bgView addSubview:tipView];
    
    
    UILabel *tipLab = [[UILabel alloc] init];
    NSString * sjlx = @"交通事故";
    for (NSDictionary * dict in self.filterArr[1]) {
        if([dict[@"dictValue"] isEqualToString:self.model.sjlx]){
            sjlx = dict[@"dictLabel"];
            break;
        }
    }
    tipLab.text = [NSString stringWithFormat:@"已启用%@%@级应急预案",sjlx,self.model.sjdj];
    tipLab.textColor = main_light_text_color_6;
    tipLab.font = [UIFont systemFontOfSize:14];
    [tipView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.centerY.offset(0);
    }];
    self.tipLab = tipLab;
    self.tableView.tableHeaderView = bgView;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.sectionHeaderTopPadding = 0;
    
    self.sectionStates = [NSMutableArray array];
    for (int i = 0; i <  self.dataArr.count; i++) {
        [self.sectionStates addObject:@(YES)]; // 默认全部展开
    }
}

#pragma mark - 展开/收起按钮点击事件

- (void)onOpenOrClose:(UIButton *)sender {
    NSInteger section = sender.tag - 10;
    
    BOOL isExpanded = [self.sectionStates[section] boolValue];
    
    // 更新状态
    self.sectionStates[section] = @(!isExpanded);
    sender.selected = !isExpanded;
    
    // 刷新对应区
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (![self.sectionStates[section] boolValue]) {
        return 0;
    }
    
    if(section < self.dataArr.count){
        YJLCModel *model = self.dataArr[section];
        NSArray *chilren = [NSArray arrayWithArray:model.children];
        return chilren.count;
        
    }
    return 0;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 55;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[PlanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (indexPath.section < self.dataArr.count) {
        
        YJLCModel *model = self.dataArr[indexPath.section];
        NSArray *chilren = [NSArray arrayWithArray:model.children];
        if(indexPath.row < chilren.count){
            NSDictionary *item = chilren[indexPath.row];
            [cell cellWithData:item];
        }

    }

    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 55)];
    
    TYBoxView *tipView = [[TYBoxView alloc] initWithFrame:CGRectMake(12, 0, kSCREEN_WIDTH-24, 40)];
    [bgView addSubview:tipView];
    YJLCModel *model = self.dataArr[section];
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.textColor = main_text_color;
    titleLab.text = [NSString stringWithFormat:@" - %@",model.gznr];
    titleLab.font = [UIFont systemFontOfSize:17];
    [tipView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
    }];
    
    UIButton *btn = [UIButton buttonWithTitle:@"收起" titleColor:main_color font:[UIFont systemFontOfSize:10] target:self action:@selector(onOpenOrClose:)];
    [btn setTitle:@"展开" forState:UIControlStateSelected];
    
    btn.selected = ![self.sectionStates[section] boolValue];
    
    btn.tag = section +10;
    [tipView addSubview:btn];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.offset(0);
        make.width.offset(60);
        make.height.offset(40);
    }];
    
    return  bgView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
